define(function(require) {

    'use strict';

    var
        _ = require('underscore'),
        $ = require('jquery'),
        Base = require('components/pop/base'),
        template = require('text!components/pop/templates/fans_level.html');

    var utils = require('core/utils');

    require('chosen');

    return Base.extend({

        template: _.template(template),

        className: 'popover bottom',

        events: {
            'click .js-btn-cancel': 'hide',
            'click .js-btn-confirm': 'triggerCallback'
        },

        initialize: function(options) {
            Base.prototype.initialize.call(this, options);
            this.data = options.data;

            var www = _global.url.www;
            this.loadLevelsUrl = www + '/weixin/autoreply/fansLevels.json';

            this.levelsData = {levels: []};
            this.loadingLevels();
        },


        loadingLevels: function(){
            var self = this;
            $.get( this.loadLevelsUrl)
                .success(function(res){
                    if(+res.code === 0){
                        self.levelsData.levels = res.data;
                        self.render();
                    } else {
                        utils.errorNotify(res.msg + '');
                    }
                })
                .fail(function(){
                    utils.errorNotify('数据获取失败');
                });
        },

        render: function() {
            var view = this;
            var levelsData = this.levelsData;
            var data = this.data.get();

            levelsData.levels.forEach(function(item){
                item.isSelected = item.id === data.id;
            });
            view.$el.html(view.template(levelsData));


            this.chosen = this.$('.js-chosen-select').chosen({
                no_results_text: '木有这个会员等级哦'
            });
            return view;
        },

        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                data = obj.data;

            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.show();

            this.$('.js-chosen-select').empty()
                .trigger('chosen:updated');
            this.loadingLevels();
        },

        setData: function(data) {
            this.data = _.extend(this.data, data);
            this.render();
        },

        positioning: function() {
            var view = this;

            view.$el.show().position({
                of: this.target,
                my: 'center top',
                at: 'center bottom',
                collision: 'none'
            });
        },

        triggerCallback: function() {
            var view = this;

            var result = [];

            this.chosen.find(":selected").each(function() {
                var item = $(this);
                result.push({
                    text: item.text(),
                    value: item.val()
                });
            });

            view.callback(result);
            view.hide();
        }
    });
});
